﻿using Dapper.Contrib.Extensions;
using IntelligentGateway.Data;
using IntelligentGateway.Model.Interface;
using IntelligentGateway.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentGateway.Services
{
    public class DaServices<T> where T : class, IBase
    {
        public ResponseMessage Delete(T t)
        {
            CommDA<T> comm = new CommDA<T>();               ;
            string result = comm.Delete(t);
            ResponseMessage message = new ResponseMessage();
            if (!result.Contains("success"))
            {
                message.errcode = 1;
                message.errmsg = result;
            }
            CommServices.SignalPointList();
            return message;
        }
        public ResponseMessage Insert(T t)
        {
            CommDA<T> comm = new CommDA<T>();
            string result = comm.Insert(t,false);
            ResponseMessage message = new ResponseMessage();
            if (!result.Contains("success"))
            {
                message.errcode = 1;
                message.errmsg = result;
            }
            CommServices.SignalPointList();
            return message;

        }
        public ResponseMessage InsertT(T t)
        {
            CommDA<T> comm = new CommDA<T>(); 
            T result = comm.InsertT(t);
            ResponseMessage message = new ResponseMessage();
            if (result != null)
            {
                message.errcode = 0;
                message.data = result;               
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "插入错误!";
            }
            return message;

        }
        public ResponseMessage SelectAllByFilter(T filter, int startindex, int endindex)
        {
            CommDA<T> comm = new CommDA<T>();
            filter.TotalRecords = filter.TotalRecords.HasValue ? (int)filter.TotalRecords : 0;
            int totalrecords = filter.TotalRecords.HasValue ? (int)filter.TotalRecords : 0;
            System.Collections.Generic.List<T> lst = comm.SelectAllByFilter(filter, startindex, endindex, ref totalrecords);

            //if(startindex >= 0&& (filter.TotalRecords==null|| filter.TotalRecords==0))
            //{                
            //    totalrecords = comm.GetTotalRecords(filter);
            //}
            ResponseMessage message = new ResponseMessage();
            if (lst != null)
            {
                message.errcode = 0;
                message.data = lst;
                message.totalrecords = totalrecords;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "查询错误!";
            }
            return message;
        }
        public ResponseMessage SelectMatchAllByFilter(T filter, int startindex, int endindex)
        {
            CommDA<T> comm = new CommDA<T>();
            filter.TotalRecords = filter.TotalRecords.HasValue ? (int)filter.TotalRecords : 0;
            int totalrecords = filter.TotalRecords.HasValue ? (int)filter.TotalRecords : 0;
            System.Collections.Generic.List<T> lst = comm.SelectMatchAllByFilter(filter, startindex, endindex, ref totalrecords);

            //if(startindex >= 0&& (filter.TotalRecords==null|| filter.TotalRecords==0))
            //{                
            //    totalrecords = comm.GetTotalRecords(filter);
            //}
            ResponseMessage message = new ResponseMessage();
            if (lst != null)
            {
                message.errcode = 0;
                message.data = lst;
                message.totalrecords = totalrecords;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "查询错误!";
            }
            return message;
        }
        public ResponseMessage SelectAllData()
        {
            CommDA<T> comm = new CommDA<T>();
            List<T> lst = comm.SelectAllData();
            ResponseMessage message = new ResponseMessage();
            if (lst != null)
            {
                message.errcode = 0;
                message.data = lst;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "查询错误!";
            }
            return message;
        }
        public ResponseMessage SelectByFilter(T filter)
        {
            CommDA<T> comm = new CommDA<T>();
            ResponseMessage message = new ResponseMessage();
            try
            {
                T t = comm.SelectByFilter(filter);
                message.errcode = 0;
                message.data = t;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "查询出错!";
            }
            return message;
        }
        public ResponseMessage SelectMatchByFilter(T filter)
        {
            CommDA<T> comm = new CommDA<T>();
            ResponseMessage message = new ResponseMessage();
            try
            {
                T t = comm.SelectMatchByFilter(filter);
                message.errcode = 0;
                message.data = t;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "查询出错!";
            }
            return message;
        }
        public ResponseMessage SelectByKey(int key)
        {
            CommDA<T> comm = new CommDA<T>();
            T t = comm.SelectByKey(key);
            ResponseMessage message = new ResponseMessage();
            if (t != null)
            {
                message.errcode = 0;
                message.data = t;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "查询错误!";
            }
            return message;
        }
        public ResponseMessage Update(T t)
        {
            CommDA<T> comm = new CommDA<T>();         
          
            string result = comm.Update(t);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;                
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
            }
            CommServices.SignalPointList();
            return message;
        }
        public ResponseMessage DeleteBatch(List<T> Ts)
        {
            ResponseMessage message = new ResponseMessage();
            foreach (T s in Ts)
            {
                message = Delete(s);
                message.errcode += message.errcode;
            }
            return message;
        }
        public ResponseMessage UpdateDemand(T t)
        {
            CommDA<T> comm = new CommDA<T>();            
            string result = comm.UpdateDemand(t);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;               
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
            }
            return message;
        }
        public ResponseMessage DeleteDemand(T t)
        {
            CommDA<T> comm = new CommDA<T>();
            Type type = typeof(T);           
            string result = comm.DeleteDemand(t);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;               
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
            }
            return message;
        }

        public List<T> SelectAllDataDis()
        {
            CommDA<T> comm = new CommDA<T>();
            List<T> lst = comm.SelectAllData();
            return lst;
        }

    }
}
